package sword_offer;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @ClassName _30BuildMinStack
 * @Description: 构建最小栈
 * @Author: yongliang.ma
 * @Create_time:2022/10/10 13:23
 */
public class _30BuildMinStack {
    public class MinStack {

        private Deque<Integer> dataStack;

        private Deque<Integer> minStack;

        /** initialize your data structure here. */
        public MinStack() {
            dataStack = new LinkedList<>();
            minStack = new LinkedList<>();
        }

        public void push(int x) {
            dataStack.push(x);
            if (minStack.isEmpty() || minStack.peek() >= x){
                minStack.push(x);
            }
        }

        public void pop() {
            if (dataStack.isEmpty())
                throw new RuntimeException("无数据可以出栈");
            int pop = dataStack.pop();
            if (pop == minStack.peek()){
                minStack.pop();
            }
        }

        public int top() {

            return dataStack.peek();
        }

        public int min() {

            return minStack.peek();
        }
    }
}
